<template>
  <x-dialog
    title="选择收货单"
    :close-on-click-modal="false"
    :show.sync="visible"
    v-if="visible"
    lock-scroll
    append-to-body
    width="1000px"
    :before-close="handleClose"
    @onCancel="handleClose"
    @onSubmit="handleSubmit"
  >
    <el-form :model="searchParams" ref="form" class="dialog-fliter">
      <el-row :gutter="20">
        <x-col>
          <el-form-item :label="$t('financialSynergy.statement.receivingNumber')">
            <el-input
              v-model="searchParams.orderNumber"
              :placeholder="$t('financialSynergy.statement.receivingNumber')"
            ></el-input>
          </el-form-item>
        </x-col>
        <x-col>
          <el-form-item :label="$t('financialSynergy.statement.materialCode')">
            <el-input
              v-model="searchParams.materialCode"
              :placeholder="$t('financialSynergy.statement.materialCode')"
            ></el-input>
          </el-form-item>
        </x-col>
        <x-col>
          <el-form-item :label="$t('financialSynergy.statement.materialName')">
            <el-input
              v-model="searchParams.materialName"
              :placeholder="$t('financialSynergy.statement.materialName')"
            ></el-input>
          </el-form-item>
        </x-col>

        <x-col>
          <div class="search-btns">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </div>
        </x-col>
      </el-row>
    </el-form>
    <div>
      <x-table
        :data-source="tableData"
        size="mini"
        @on-selection-change="handleSelectionChange"
        @on-change="handleTableChange"
        :pagination="pagination"
        :loading="loading"
        :row-key="'id'"
        :height="430"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :prop="item.prop"
          :fixed="item.fixed"
          :show-overflow-tooltip="true"
          :width="item.width || 100"
          :label="item.label"
          :sortable="'custom'"
        >
          <template #default="scope">
            <span v-if="item.prop === 'state'">
              {{ '已收货' }}
            </span>
            <span v-else-if="item.prop === 'materialUnit'">{{selectDictLabel(dictOptions['srm_material.material_unit'], scope.row[item.prop])}}</span>
            <span v-else>{{ scope.row[item.prop] | emptyVal }}</span>
          </template>
        </el-table-column>
      </x-table>
    </div>
  </x-dialog>
</template>
<script>
  import { TableListMixin, QueryDictMixin, PermissionsMixin, selectDictLabel } from '@package/utils'
  import { columns } from '@/modules/financialSynergy/config/purchaseOrder'
  import { deliveryNoteDetailUrl as listUrl } from '@/modules/financialSynergy/api/statementAccount.js'
  import { EnumApiCode } from '../../../../../../globalEnum.js'

  export default {
    components: {},
    mixins: [TableListMixin, QueryDictMixin, PermissionsMixin],
    props: {
      title: {
        type: String,
        default: ''
      },
      placeholder: {
        type: String,
        default: ''
      },
      supplierId: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        listUrl,
        columns,
        visible: false,
        dictKeys: ['system.preset'],
        srmDictKeys: ['srm_material.material_unit'],
        searchParams: {
          materialCode: '',
          materialName: '',
          orderNumber: '',
          state: 3,
          billState: '0',
          supplierId: ''
        }
      }
    },
    computed: {
      rules() {}
    },
    watch: {
      supplierId: {
        handler(val) {
          this.searchParams.supplierId = val
        },
        deep: true,
        immediate: true
      }
    },
    created() {
      this.initDictConfig()
    },
    methods: {
      selectDictLabel,
      show() {
        this.visible = true
        this.queryParam = { ...this.searchParams, state: 3, billState: '0' }
        this.loadData()
      },
      handleSearch(val) {
        this.queryParam = { ...this.searchParams, ...val, state: 3, billState: '0' }
        this.loadData()
      },
      handleReset() {
        this.searchParams = {
          materialCode: '',
          materialName: '',
          orderNumber: '',
          state: 3,
          billState: '0',
          supplierId: this.searchParams.supplierId || this.supplierId
        }
        this.queryParam = { ...this.searchParams }
        this.loadData()
      },
      handleClose() {
        this.selectData = []

        this.visible = false
      },
      // 多行选中
      handleSelectionChange(rows) {
        this.selectData = rows
      },
      // 弹窗确定
      handleSubmit() {
        if (this.selectData.length) {
          this.$emit('on-confirm', this.selectData)
          this.handleClose()
        } else {
          this.$message.error('请至少选择一条数据点击确定')
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .search-btns {
    align-items: flex-end;
    display: flex;
    height: 70px;
  }
</style>
